﻿namespace NodeBasedSkillEditor
{
    public class Value_Scalar : AValue
    {
        public override EValueType ValueType { get { return EValueType.Scalar; } }

        public float Value;

        public Value_Scalar(float v)
        {
            Value = v;
        }

        public Value_Scalar(FloatPort portData)
        {
            Value = portData.Value;
        }

        public override AValue Clone()
        {
            return new Value_Scalar(Value);
        }

        public override (bool, bool) ToBoolean()
        {
            return (true, Value != 0);
        }

        public override bool SetBoolean(bool v)
        {
            Value = v ? 1 : 0;
            return true;
        }

        public override (bool, int) ToInteger()
        {
            return (true, (int)Value);
        }

        public override bool SetInteger(int v)
        {
            Value = v;
            return true;
        }

        public override (bool, float) ToScalar()
        {
            return (true, Value);
        }

        public override bool SetScalar(float v)
        {
            Value = v;
            return true;
        }

        public override void Reset()
        {
            Value = 0;
        }

        public override void Negative()
        {
            Value = -Value;
        }

        public override void Add(AValue x)
        {
            var v = x.ToScalar();
            if (v.Item1)
            {
                Value = Value + v.Item2;
            }
        }

        public override void Subtract(AValue x)
        {
            var v = x.ToScalar();
            if (v.Item1)
            {
                Value = Value - v.Item2;
            }
        }

        public override void Multiply(AValue x)
        {
            var v = x.ToScalar();
            if (v.Item1)
            {
                Value = Value * v.Item2;
            }
        }

        public override void DivideBy(AValue x)
        {
            var v = x.ToScalar();
            if (v.Item1)
            {
                Value = Value / v.Item2;
            }
        }

        public override void Modulo(AValue x)
        {
            var v = x.ToScalar();
            if (v.Item1)
            {
                Value = Value % v.Item2;
            }
        }

        public override bool EqualsValue(AValue x)
        {
            var v = x.ToScalar();
            if (v.Item1)
            {
                return Value == v.Item2;
            }
            return false;
        }

        public override string ToString()
        {
            return Value.ToString();
        }
    }
}
